package com.caltco.cargo.ops.modules.sys.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.caltco.cargo.ops.auth.bo.AdminUserDetails;
import com.caltco.cargo.ops.common.base.gen.BaseServiceImpl;
import com.caltco.cargo.ops.common.exception.RRException;
import com.caltco.cargo.ops.modules.sys.dao.*;
import com.caltco.cargo.ops.modules.sys.entity.DailyTask;
import com.caltco.cargo.ops.modules.sys.entity.LearnWord;
import com.caltco.cargo.ops.modules.sys.entity.LevelValue;
import com.caltco.cargo.ops.modules.sys.entity.UserLevel;
import com.caltco.cargo.ops.modules.sys.request.LearnWordRequest;
import com.caltco.cargo.ops.modules.sys.request.UpdateLevelRequest;
import com.caltco.cargo.ops.modules.sys.service.LearnWordService;
import com.caltco.cargo.ops.modules.sys.vo.UserLevelVO;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import java.sql.Date;
import java.time.LocalDate;
import java.util.List;

@Service
public class LearnWordServiceImp extends BaseServiceImpl<LearnWordDao, LearnWord> implements LearnWordService {
    private final LearnWordDao learnWordDao;
    private final UserLevelDao userLevelDao;
    private final LevelValueDao levelValueDao;
    private final WordServiceImp wordServiceImp;
    private final DailyTaskDao dailyTaskDao;


    public LearnWordServiceImp(LearnWordDao learnWordDao, UserLevelDao userLevelDao, LevelValueDao levelValueDao, FrequencyStatisticsDao frequencyStatisticsDao, WordServiceImp wordServiceImp, DailyTaskDao dailyTaskDao) {
        this.learnWordDao = learnWordDao;
        this.userLevelDao = userLevelDao;
        this.levelValueDao = levelValueDao;
        this.wordServiceImp = wordServiceImp;
        this.dailyTaskDao = dailyTaskDao;
    }

    public Date getToday() {
        return Date.valueOf(LocalDate.now());  // 获取当天日期（YYYY-MM-DD）
    }

    public Long getUserIdByToken() {
        return ((AdminUserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getUserEntity().getUserId();
    }

    @Override
    public Boolean getWordByType(LearnWordRequest request) {

        Integer model = request.getModel();
        LearnWord learnWord = new LearnWord();
        learnWord.setWordId(request.getWordId());
        learnWord.setUserId(getUserIdByToken());
        learnWord.setModel(request.getModel());
        learnWord.setDate(getToday());
        learnWord.setWordType(request.getWordType());
        learnWord.setIsCurrent(request.getIsCurrent());
        wordServiceImp.updateReviewStatus(request.getWordId(), request.getIsCurrent(), request.getWordType());
        learnWordDao.insert(learnWord);
        return true;
    }

    @Override
    public UserLevelVO getLevel() {
        UserLevel userLevel = userLevelDao.selectOne(new LambdaQueryWrapper<UserLevel>()
                .eq(UserLevel::getUserId, getUserIdByToken()));

        Integer level = levelValueDao.selectOne(new LambdaQueryWrapper<LevelValue>()
                .le(LevelValue::getLevelValue, userLevel.getLevelExp()) // 找到经验值 <= 当前经验值的最高等级
                .orderByDesc(LevelValue::getLevelValue)  // 按经验值倒序
                .last("LIMIT 1")).getLevelNumber();
        UserLevelVO userLevelVO = new UserLevelVO();
        BeanUtil.copyProperties(userLevel, userLevelVO);
        userLevelVO.setLevel(level);
        return userLevelVO;
    }

    @Override
    public Boolean updateLevel(UpdateLevelRequest request) {
        UserLevel userLevel = userLevelDao.selectOne(new LambdaQueryWrapper<UserLevel>()
                .eq(UserLevel::getUserId, getUserIdByToken()));
        if (userLevel.getHp() < 1){
            throw new RRException("您的体力不足");
        }
        if (request.getIsCurrent() == 0){
            userLevel.setHp(userLevel.getLevelExp() - 1);
            userLevel.setLevelExp(200);
            userLevel.setPoint(50);
        }
        return true;
    }

    @Override
    public List<UserLevelVO> getLeaderboard() {
        return userLevelDao.getAllUserLevels();
    }
}
